<?php

namespace app\services\order;

use app\dao\order\OrderBonusDao;
use app\services\BaseServices;
use app\services\user\UserMoneyServices;
use app\services\user\UserServices;

class OrderBonusServices extends BaseServices
{

    /**
     * 初始化，获得dao层句柄
     * OtherOrderServices constructor.
     * @param OrderBonusDao $dao
     */
    public function __construct(OrderBonusDao $dao)
    {
        $this->dao = $dao;
    }

    public function addOrderBonus($oid, $uid, $integral)
    {
        $data['oid'] = $oid;
        $data['uid'] = $uid;
        $data['integral'] = $integral;
        $data['add_time'] = time();
        $this->dao->save($data);
    }

    /**
     * @param $order
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cancel($order)
    {
        $log = $this->dao->getModel()->where('oid', $order['id'])->find();
        if (isset($log['uid']) && $log['uid']){
            /** @var UserServices $userSrv */
            $userSrv = app()->make(UserServices::class);
            $user = $userSrv->getUserInfo($log['uid']);
            $balance = bcsub($user['integral'], $log['integral'], 0);
            $userSrv->update($log['uid'], ['integral'=>$balance]);
            $this->dao->update($log['id'], ['is_over'=>1, 'is_first'=>1]);
        }
    }

    public function firstBonus()
    {
        $start = strtotime(date('Y-m-d 00:00:00', strtotime('-58 days')));
        $end = $start + (3600 * 24);
        /** @var UserServices $userSrv */
        $userSrv = app()->make(UserServices::class);
        $this->dao->getModel()->where('is_first', 0)->where('is_over', 0)
            ->where('add_time', '>', $start)->where('add_time', '<', $end)
            ->chunk(100, function($list) use ($userSrv){
                if (!empty($list)){
                    $list = $list->toArray();
                    foreach ($list as $order){
                        $userSrv->addBonus($order['uid'], $order['integral'], $order['oid']);
                        $this->dao->update($order['id'], ['is_first'=>1, 'last_time'=>time()]);
                    }
                }
            });
    }

    public function monthBonus()
    {
        $start = strtotime(date('Y-m-d 00:00:00', strtotime('-30 days')));
        $end = $start + (3600 * 24);
        /** @var UserServices $userSrv */
        $userSrv = app()->make(UserServices::class);
        $this->dao->getModel()->where('is_first', 1)->where('is_over', 0)
            ->where('last_time', '>', $start)->where('last_time', '<', $end)
            ->chunk(100, function($list) use ($userSrv){
                if (!empty($list)){
                    $list = $list->toArray();
                    foreach ($list as $order){
                        $isOver = $userSrv->addBonus($order['uid'], $order['integral'], $order['oid']);
                        $update = [];
                        $update['last_time'] = time();
                        if ($isOver){
                            $update['is_over'] = 1;
                            $update['over_time'] = time();
                        }
                        $this->dao->update($order['id'], $update);
                    }
                }
            });
    }
}